﻿using System;
using System.Diagnostics;

namespace WindLoad
{
    public static class StringExtensions
    {
        public static string Left(this string input, int length)
        {
            Debug.Assert(input != null && 0 <= length && length <= input.Length);
            var result = input.Substring(0, length);
            return result;
        }

        public static string LeftOf(this string input, char character)
        {
            return LeftOf(input, character, 0);
        }

        public static string LeftOf(this string input, char character, int skip)
        {
            Debug.Assert(input != null && skip >= 0);

            string result;
            if (input.Length == 0)
            {
                result = input;
            }
            else
            {
                var characterPosition = 0;
                var charactersFound = -1;
                while (charactersFound < skip)
                {
                    characterPosition = input.IndexOf(character, characterPosition + 1);
                    if (characterPosition == -1)
                    {
                        break;
                    }
                    else
                    {
                        charactersFound++;
                    }
                }

                if (characterPosition == -1)
                {
                    result = input;
                }
                else
                {
                    result = input.Substring(0, characterPosition);
                }
            }

            return result;
        }

        public static string LeftOf(this string input, string value)
        {
            return LeftOf(input, value, StringComparison.Ordinal);
        }

        public static string LeftOf(this string input, string value, StringComparison comparisonType)
        {
            return LeftOf(input, value, 0, comparisonType);
        }

        public static string LeftOf(this string input, string value, int skip, StringComparison comparisonType)
        {
            Debug.Assert(input != null && value != null && skip >= 0);

            string result;
            if (input.Length <= skip)
            {
                result = input;
            }
            else
            {
                var valuePosition = 0;
                var valuesFound = -1;
                while (valuesFound < skip)
                {
                    valuePosition = input.IndexOf(value, valuePosition + 1, comparisonType);
                    if (valuePosition == -1)
                    {
                        break;
                    }
                    else
                    {
                        valuesFound++;
                    }
                }

                if (valuePosition == -1)
                {
                    result = input;
                }
                else
                {
                    result = input.Substring(0, valuePosition);
                }
            }

            return result;
        }

        public static bool EqualTo(this string first, string second,
            StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
            => string.Compare(first, second, comparisonType) == 0;
    }
}